from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import sys
from time import sleep
import LogHelper

logger = None  # 日志

def autoFillText(driver,index,name,number,address):
	global logger
	try:
		logger.logger.info("当前输入第"+str(index)+"条数据"+ name)
		driver.get('http://tiaojie.court.gov.cn/fayuan/preCase/list')
		sleep(2)
		driver.find_element_by_id('assigned').click()
		#driver.find_element_by_xpath('//*[@id="assigned"]').click()
		logger.logger.info('等待加载')
		sleep(4)
		logger.logger.info('等待结束，设置显示复制案件')
		driver.execute_script("document.getElementById('copy_case').style.display = 'block';")
		logger.logger.info('等待复制案件按钮显示')
		sleep(3)
		logger.logger.info('等待复制案件按钮显示完成，点击按钮')
		driver.find_element_by_id('copy_case').click()
		#driver.find_element_by_xpath('//*[@id="check-update"]/a[3]').click()
		logger.logger.info('跳转到详情页面开始')
		sleep(5)
		logger.logger.info('跳转到详情页面加载完成')
		driver.switch_to_window(driver.current_window_handle)	  
		logger.logger.info('切换到新页面')
		sleep(3)
		logger.logger.info('切换到新页面完成，输入内容')
		driver.find_element_by_name('respondentName').clear()
		driver.find_element_by_name('respondentAddress').clear()
		driver.find_element_by_name('respondentIDCardNo').clear()
		logger.logger.info('清空输入框')
		driver.find_element_by_name('respondentName').send_keys(name)
		driver.find_element_by_name('respondentAddress').send_keys(address)
		driver.find_element_by_name('respondentIDCardNo').send_keys(number)
		sleep(1)
		logger.logger.info('点击提交')
		driver.find_element_by_id('submit').click()
		logger.logger.info('弹出是否相同电话号码确认框')
		sleep(2)
		logger.logger.info('点击确认相同电话号码')
		driver.find_element_by_xpath('//*[@id="check-update"]/a[3]').click()
		logger.logger.info('弹出新增案件确认框')
		sleep(2)
		driver.find_element_by_xpath('//*[@id="check-update"]/a[3]').click()
		logger.logger.info('点击确认新增案件')
	except Exception as e:
		logger.logger.error('第'+str(index)+'条数据name='+str(name)+'发生错误！！'+str(e))


def main():
	global logger
	logger = LogHelper.LogHelper("TextPdf.log")
	logger.logger.info('启动执行')
	logger.logger.info('Enter开始自动化')
	s = input()
	options = Options()
	options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
	driver = webdriver.Chrome(executable_path='chromedriver.exe', options=options)

	data=None
	with open("info.txt", "r",encoding='utf-8') as f:
		data = f.readlines()
	if data!=None:
		index = 1;
		for item in data:
			info = item.split('\t')
			autoFillText(driver,index,info[0],info[1],info[2])
			index+=1
	

if __name__ == "__main__":
    main()